package com.guaoran.other.singleton.register;


import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author : 孤傲然
 * @Description :
 * Spring中的做法，就是用这种注册式单例
 * @date :2018/5/17 14:07
 */
public class BeanFactory {
    private BeanFactory(){}

    private final static Map<String,Object> ioc = new ConcurrentHashMap<String, Object>();

    public static Object getBean(String className){
        if(!ioc.containsKey(className)){
            Object o = null;
            try {
                o = Class.forName(className).newInstance();
                ioc.put(className,o);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return o;
        }else{
            return ioc.get(className);
        }
    }
}
